Running Python on Your Computer
Lesson Objectivesβ
After this lesson, you will be able toβ¦
- Write Python 3 scripts locally.
- Run Python 3 scripts via the command prompt.
Defining Assumptionsβ
We can't use repl.it forever! We can run Python right on our computers.
- GA uses Macs for its default curriculum.
- We are happy to help you program Python on Windows or Linux as well.
- Sometimes you may need to ask questions about subtle differences!
Let's Get Installingβ
This will take us 10 to 15 minutes, then we'll regroup.
First, install Visual Studio Code from https://code.visualstudio.com/download. This is where you'll write your code.
Then, follow the Python 3 installation directions by brew install python.
Regroupβ
- You have Python 3 installed!
- You may have seen the in-line REPL (we'll revisit!).
- You have a text editor β Visual Studio Code β installed.
- You are ready to create a local
.pyfile and run it.
In-Line REPLβ
Let's check out a REPL.
Read-Evaluate-Print-Loop.- An interactive way to code.
Let's do it!
- Open your terminal.
- Then, open your in-line REPL:
Mac/Linux:
python
Windows:
py
We Do: Interactive Developmentβ
You can type any Python code you want here. Let's declare a variable and do some math:
x = 4
y = 5
print(x + y)
Pro tip:
exit()is the command for when you want to get out of this environment!
We Do: Local Filesβ
We can create a file with a .py extension β we can execute properly written Python code in a .py file.
- Create a new file with a
.pyextension. Let's call itmy_file.py. - Open
my_file.pyinAtom. - In this file, declare some variables. Let's mix integers and strings!
favorite_tv_show = "Ninja Warrior"
obstacles_cleared = 5
time = "3 min, 20 sec"
print("I cleared", obstacles_cleared, "on", favorite_tv_show, "in", time)
Save and close your file.
Pro tip: Make sure you have a
We Do: Running Local Filesβ
Now we have code in a file, but we need to run it.
Back on your terminal, we'll navigate to where that file is located:
cdstands for "change directory" ("directory" is another word for "folder").cd my_folderchanges to the directorymy_directory.cd ..goes up to the parent folder of the one you're in.
Running the file varies slightly between Windows and Mac/Linux:
- Mac/Linux:
python my_file.py - Windows:
py my_file.py
- Mac/Linux:
Raise your hand if you need help!
Summaryβ
- We now have Python 3 β the latest and greatest β installed.
- We can use our in-line REPL in the terminal.
- We can execute a local
.pyfile with Python code in it. - We know how to do this regardless of whether we use Mac, Linux, or Windows.
Any questions?